#Requires AutoHotkey v2.0
#Include G:\AHK\git-ahk-lib\lib\component\ColorExtractor.ahk

class Setting extends Gui {
  static ins := Setting(cfg)

  __New(cfg) {
    super.__New('+AlwaysOnTop')
    this.SetFont('s13', 'Microsoft YaHei UI')
    tab := this.AddTab3('w300', ["截图框", '遮罩', "贴图", "其他"])
    tab.UseTab(1)
    this.AddCheckbox('VT1C1 Section', '使用透明色')
      .OnEvent('Click', (obj, *) => this.OnClick1(obj, this['T1E1'], this['T1E2']))
    this.AddCheckbox('yp VT1C2', '显示静态背景').OnEvent('Click', (*) => cfg.showStaticBg := this['T1C2'].Value)
    this.AddText('xs', '截图背景色:')
    this.AddEdit('yp vT1E0 w60 Background' cfg.guiBgc,)
    this.AddButton('yp h30 vT1E1', cfg.guiBgc).OnEvent('Click', (*) => this._getColor('T1E1', 'T1E0'))
    this.AddText('xs', '背景透明度:')
    this.AddEdit('yp vT1E2 w60', cfg.guiTransparent)
    tab.UseTab(2)
    this.AddCheckbox('vT2C1', '显示遮罩')
      .OnEvent('Click', (obj, *) => this.OnClick2(obj, this['T2E1'], this['T2E2']))
    this.AddText('Section vT2C', '遮罩背景色:')
    this.AddEdit('yp vT2E0 w60 Background' cfg.maskBgc,)
    this.AddButton('yp h30 vT2E1', cfg.maskBgc).OnEvent('Click', (*) => this._getColor('T2E1', 'T2E0'))
    this.AddText('xs', '遮罩透明度:')
    this.AddEdit('yp vT2E2  w60', cfg.maskInitTransparent)
    tab.UseTab(3)
    this.AddCheckbox('vT3C1', '立即贴图').OnEvent('Click', (*) => cfg.pasteInstantly := this['T3C1'].Value)
    this.AddCheckbox('vT3C3', '贴图后保存为历史').OnEvent('Click', (*) => cfg.autoSave := this['T3C3'].Value)

    this.OnEvent('Close', (*) => this.OnDestroy())
    tab.UseTab(4)
    this.AddCheckbox('vT4C1', '截图时显示更多信息')
      .OnEvent('Click', (obj, *) => cfg.withTip := obj.Value)
    this.Init()
  }

  _getColor(ctl1, ctl2) {
    rgb := (ColorExtractor('0x' this[ctl1].Text, this.Hwnd).RGB).substring(3)
    this[ctl2].Opt('Background' rgb)
    this[ctl1].Text := rgb
  }

  Diff(endState) {
    loop this.initState.Length {
      if this.initState[A_Index] != endState[A_Index] {
        this.initState := endState
        return true
      }
    }
    return false
  }

  OnChange(obj, target) {
    if StrLen(obj.Text) = 6 {
      target.Opt('+Background' obj.Text)
      obj.Opt('cblack')
    }
    else {
      obj.Opt('cRed')
    }
  }

  static Show(*) {
    Setting.ins.Show()
  }

  Init() {
    if cfg.noBgc || cfg.useRandomBgc {
      this['T1C1'].Value := 1
      this['T1E1'].Opt('+ReadOnly cRed')
      this['T1E2'].Opt('+ReadOnly cRed')
    }
    this['T1C2'].Value := cfg.showStaticBg
    if cfg.showMask {
      this['T2C1'].Value := 1
    }
    else {
      this['T2E1'].Opt('+ReadOnly cRed')
      this['T2E2'].Opt('+ReadOnly cRed')
    }
    this['T3C1'].Value := cfg.pasteInstantly
    this['T3C3'].Value := cfg.autoSave
    this['T4C1'].Value := cfg.withTip
    this.initState := Array(cfg.noBgc, cfg.showStaticBg, cfg.guiBgc, cfg.guiTransparent
      , cfg.showMask, cfg.maskBgc, cfg.maskInitTransparent
      , cfg.pasteInstantly, cfg.autoSave
      , cfg.withTip)
  }

  OnClick1(obj, t1, t2) {
    if obj.Value {
      t1.Opt('+ReadOnly cRed')
      t2.Opt('+ReadOnly cRed')
      cfg.noBgc := true
    } else {
      t1.Opt('-ReadOnly cBlack')
      t2.Opt('-ReadOnly cBlack')
      cfg.noBgc := false
    }
  }

  OnClick2(obj, t1, t2) {
    if obj.Value {
      t1.Opt('-ReadOnly cBlack')
      t2.Opt('-ReadOnly cBlack')
      cfg.showMask := true
    } else {
      t1.Opt('+ReadOnly cRed')
      t2.Opt('+ReadOnly cRed')
      cfg.showMask := false
    }
  }

  OnDestroy(*) {
    endState := Array(this['T1C1'].Value, this['T1C2'].Value, this['T1E1'].Text, this['T1E2'].Text
      , this['T2C1'].Value, this['T2E1'].Text, this['T2E2'].Text
      , this['T3C1'].Value, this['T3C3'].Value
      , this['T4C1'].Value
    )
    if !this.Diff(endState)
      return
    cfs.Set('noBgc', this['T1C1'].Value)
    cfs.Set('staticBG', this['T1C2'].Value)
    if StrLen(this['T1E1'].Text) = 6 {
      cfs.Set('guiBgc', this['T1E1'].Text)
      cfg.guiBgc := this['T1E1'].Text
    }
    if this['T1E2'].Text > 0 && this['T1E2'].Text <= 255 {
      cfs.Set('guiTRP', this['T1E2'].Text)
      cfg.guiTransparent := this['T1E2'].Text
    }
    cfs.Set('showMask', this['T2C1'].Value)
    if StrLen(this['T2E1'].Text) = 6 {
      cfs.Set('maskBgc', this['T2E1'].Text)
      cfg.maskBgc := this['T2E1'].Text
    }
    if this['T2E2'].Text > 0 && this['T2E2'].Text <= 255 {
      cfs.Set('maskTRP', this['T2E2'].Text)
      cfg.maskInitTransparent := this['T2E2'].Text
    }
    cfs.Set('pasteIST', this['T3C1'].Value)
    cfs.Set('autoSave', this['T3C3'].Value)
    cfs.Set('tip', this['T4C1'].Value)
    cfs.Sync()
  }
}